<!DOCTYPE html>
<html lang="zh_CN" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="utf-8">
    <title>提醒中心 Notification Center</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, config-scalable=0">
    <link rel="stylesheet" th:href="@{/statics/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/statics/style/admin.css}" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">提醒中心</div>
                <div class="layui-card-body">
                    <table class="layui-hide" id="notification-list-table" lay-filter="notification-list-filter"></table>

                    <script type="text/html" id="notification-list-table-toolbar">
                        <div class="layui-btn-container">
                            <!--button shiro:hasPermission="cms:notification:setting" class="layui-btn layui-btn-sm" lay-event="setting">
                                <i class="layui-icon layui-icon-set"></i> 提醒设置
                            </button-->
                            <button shiro:hasPermission="cms:notification:remove" class="layui-btn layui-btn-sm" lay-event="clear">
                                清空
                            </button>
                        </div>
                    </script>
                    <blockquote class="layui-elem-quote">
                        <div style="margin-bottom: 10px;">
                            已阅读的提醒仅会保留<span style="color: #1E9FFF"><b>30天</b></span>，过期会被清理；
                        </div>
                    </blockquote>
                </div>
            </div>
        </div>
    </div>
</div>


<script th:src="@{/statics/layui/layui.js}"></script>
<script th:inline="none">
    layui.config({
        base: '/statics/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table'], function () {
        var $ = layui.$
            , table = layui.table;

        var tableIns = table.render({
            elem: '#notification-list-table'
            , url: '/api/notification/page'
            , id: 'id'
            , toolbar: '#notification-list-table-toolbar'
            , cols: [[
               {
                    field: 'isRead'
                   ,width: 40
                    ,templet: function (d) {
                        if (d.isRead == 0) {
                            return '<span class="layui-badge-dot layui-bg-green"></span>';
                        }
                        return '';
                    }
                }
                , {
                    field: 'icon'
                    ,width: 45
                    , templet: function (d) {
                        return '<i class="fa ' + d.icon + '"></i>';
                    }
                }
                , {
                    field: 'title', title: '标题', templet: function (d) {
                        return '<span class="'+ d.level +'">' + d.title + '</span>';
                    }
                }
                , {field: 'createTime', width: 160, title: '时间'}
            ]]
            , page: true
            , request: {
                pageName: 'pageNum'
                , limitName: 'pageSize'
            }
        });


        //头工具栏事件
        table.on('toolbar(notification-list-filter)', function (obj) {
            var checkStatus = table.checkStatus('id')
                , data = checkStatus.data;
            switch (obj.event) {
                case 'clear':
                    layer.confirm('真的删除么', function (index) {
                        $.ajax({
                            url: '/api/notification/clear',
                            method: 'DELETE',
                            dataType: 'JSON',
                            success: function (res) {
                                if (res.code == 0) {
                                    tableIns.reload({page: {curr: 1}}); //数据刷新
                                    layer.msg('记录已清空！', {icon: 1, time: 1000}, function () {
                                        layer.close(index); //关闭弹层
                                    });
                                } else {
                                    layer.msg(res.msg);
                                }
                            },
                            error: function (err) {
                            }
                        });
                    });
                    break;
                case 'setting':
                    var index = layer.open({
                        type: 2
                        , title: '提醒设置'
                        , content: '/cms/my/notification/setting'
                        , maxmin: false
                        , area: ['600px', '450px']
                        , btn: ['发送', '关闭']
                        , yes: function (index, layero) {
                            var iframeWindow = window['layui-layer-iframe' + index]
                                , submitID = 'LAY-notification-add-submit'
                                , submit = layero.find('iframe').contents().find('#' + submitID);

                            //监听提交
                            iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                                //提交 Ajax 成功后，静态更新表格中的数据
                                $.ajax({
                                    url: '/api/notification/setting',
                                    method: 'post',
                                    data: data.field,
                                    dataType: 'JSON',
                                    success: function (res) {
                                        if (res.code == 0) {
                                            tableIns.reload(); //数据刷新
                                            layer.close(index); //关闭弹层
                                        } else {
                                            alert(res.msg);
                                        }
                                    },
                                    error: function (err) {
                                    }
                                });

                            });
                            submit.trigger('click');
                        }
                    });
                    break;
            }
        });

    });
</script>
</body>
</html>